/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package SearchFile;



import java.io.File;
import java.io.FileFilter;


/**
 *
 * @author Alex
 */
public class SearchFile {
    
    private String _strSearchFor;
    
    private File _dirSearchIn;
    
    private FileFilter _filter;
    
    private SearchFileListener _searchListener;
    
    
    public SearchFile()
    {
        _strSearchFor = "";
        _dirSearchIn = new File(".");
    }
    
    public SearchFile(String strSearchFor, File dirSearchIn, FileFilter filter)
    {
        this._strSearchFor = strSearchFor;
        this._dirSearchIn = dirSearchIn;
        this._filter = filter;
    }
    
    
    
    
    
    
    /// Get/Set Properties 
    ///
    public FileFilter getFilter()
    {
        return _filter;
    }
    
    public void setFilter(FileFilter filter)
    {
        this._filter = filter;
    }
    
    
    /// End Get/Set Properties
    
    public void startSearch()
    {
        visitAllDirsAndFiles(_dirSearchIn);
        _searchListener.whenDone();
    }
    
    
    private void visitAllDirsAndFiles(File dir)
    {
        
        try{
            Thread.sleep(10);
        }catch(InterruptedException ex)
        {
            
        }
        
        
        if (dir.isDirectory())
        {
            // ngdlong - temp remove, will revert soon !
//            File[] children = DirectoryItem.listCholdFolders(dir);
//            for(File e1:children)
//            {
//                _searchListener.lookingForItem(e1.getPath());
//                
//                File[] fs = e1.listFiles(_filter);
//                for (File e2:fs)
//                {
//                    _searchListener.foundAItem(e2.getAbsolutePath());
//                    
//                    visitAllDirsAndFiles(e1);
//                }
//            }
        }
        
        
    }
    
    
    public void addSearchListener(SearchFileListener listener)
    {
        _searchListener = listener;
    }
    
    
    public void removeSearchingListener()
    {
        _searchListener = null;
    }
    
    
}
